BASHing Through Scripts

Written by Ken Gypen

November 26, 2007 | 08:12

Tags: #script #shell #unix

Companies: #bash

Scripting for the real world

So now that we've covered some basic syntax and techniques, let's look at something that's actually useful. I'm going to walk you through a script that I wrote some time ago for Brett, to resize and watermark all the images from his articles. And for those Windows users who are in doubt - yes, you can do image editing from a command line and it isn’t that hard either. You just need one thing - a program called ImageMagick.

So before we can get our elbows dirty in scripting, we should first be familiar with ImageMagick. There are three main commands that need our attention, identify, convert and composite. Put easy, identify gets information about an image, convert alters an image (cropping, resizing and the likes) and composite merges two or more images together. Of course, each of those commands has its own specific syntax. But don’t worry about that too much, the syntax is pretty straightforward.

Image converting

So, we are going to create a script that is able should:
  • Identify if an image is landscape or portrait.
  • Resize the image accordingly, preserving the original.
  • Add a watermark to the resized image.

Now, let's start at step one - identifying if it’s portrait or landscape. First we create two variables, ‘height’ and ‘width’ and fill them with the correct information. The key command in this step is, like you’ve guessed, identify.

#! /bin/bash

i="$1"

height=`identify -format %h ${i}`
width=`identify -format %w ${i}`

echo "${width} x ${height}"

So, what do we have here? First, we use a trick which allows us to test our script - we take the first command line argument and use it as our main variable (i="$1") This might sound weird, but it’ll show to be a neat trick in the end. Then we define two variables, ‘height’ and ‘width’, which get filled in accordingly. For testing purposes we now quickly echo our variables. Go ahead, test it! Save your script as “imageconvert.sh” and place it in the same directory as your image, which we'll call BASH.png.
sh imageconvert.sh BASH.png
If all goes well, your script will reply a decent “1024 x 768” or the like. So, we know it works now, and can safely remove the last echo.

BASHing Through Scripts Playing with pictures BASHing Through Scripts Playing with pictures
Click to enlarge.

So far so good, now we include some logic to determine if we are in landscape or portrait mode. In portrait, width < height, and visa versa for landscape. This is done through an easy ‘if’ statement, which we’ll append to the bottom of our script.

if [ ${width} -lt ${height} ]
then
	mode=portrait
else
	mode=landscape
fi

echo ${mode}
Now, use the same command as before to test the script, and it should return “landscape”. So all is well and we can remove the last echo once again!
Discuss this in the forums
YouTube logo
MSI MPG Velox 100R Chassis Review

October 14 2021 | 15:04